﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCS.DAL;
using SCS.Common.Models;
using System.Linq.Expressions;
namespace SCS.Business
{
    public class SchoolYearServices
    {
        IUnitOfWork _unitOfWork;
        public SchoolYearServices()
        {
            _unitOfWork = new UnitOfWork();
        }

        /// <summary>
        /// AddSchoolYear
        /// </summary>
        /// <param name="schoolYear">schoolYear</param>
        public void AddSchoolYear(SchoolYear schoolYear)
        {
            int id = _unitOfWork.SchoolYear.GetAll().Max(t => (int?)t.schoolYearID) ?? 0;
            schoolYear.schoolYearID = id + 1;
            _unitOfWork.SchoolYear.Add(schoolYear);
            _unitOfWork.Commit();
        }

        public int GetMaxSchoolYear()
        {
            return _unitOfWork.SchoolYear.GetAll().Select(m => m.schoolYearID).Max();
        }
        public List<ClassInSchoolYear> GetAllClassInASchoolYear(int schoolYear)
        {
            return _unitOfWork.ClassInSchoolYear.Get(m => m.schoolYearID == schoolYear).ToList();
        }
        /// <summary>
        /// CheckSchoolYearName
        /// </summary>
        /// <param name="schoolYear">schoolYear</param>
        /// <returns>bool</returns>
        public bool CheckSchoolYearName(SchoolYear schoolYear)
        {
            Expression<Func<SchoolYear, bool>> predicate = (p => p.schoolYearName == schoolYear.schoolYearName);
            int count = _unitOfWork.SchoolYear.Get(predicate).ToList<SchoolYear>().Count;
            DateTime? _startDate = schoolYear.startDate;
            DateTime? _endDate = schoolYear.endDate;

            if (count == 0 && _startDate <= _endDate)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// GetAll school year
        /// </summary>
        /// <returns>school year list</returns>
        public List<SchoolYear> GetAll()
        {
            return _unitOfWork.SchoolYear.GetAll().ToList<SchoolYear>();
        }

        /// <summary>
        /// GetCount
        /// </summary>
        /// <returns>count</returns>
        public int GetCount()
        {
            return 1;// _unitOfWork.SchoolYear.GetCount();
        }

        /// <summary>
        /// ChangeClassInNewSchoolYear
        /// </summary>
        /// <returns>school year</returns>
        public void ChangeClassInNewSchoolYear(List<ClassInSchoolYear> _class)
        {
            int id = _unitOfWork.ClassInSchoolYear.GetAll().Max(t => (int?)t.classInSchoolYearID) ?? 0;

            foreach (var item in _class)
            {
                id++;
                item.classInSchoolYearID = id;
                _unitOfWork.ClassInSchoolYear.Add(item);
            }
            _unitOfWork.Commit();
        }
        /// <summary>
        /// GetSchoolYear_Last
        /// </summary>
        /// <returns>school year</returns>
        public SchoolYear GetSchoolYear_Last()
        {
            int id = _unitOfWork.SchoolYear.GetAll().Max(t => (int?)t.schoolYearID) ?? 0;
            return _unitOfWork.SchoolYear.Get(m => m.schoolYearID == id).FirstOrDefault();
        }
    }
}